﻿using System;
using System.Collections.Generic;
using System.Text;
using MarcXmlParser;

namespace MarcXmlParserTutorials
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Create new marcxml record with some fields");
            CRecord myRec = new CRecord();
            myRec.Leader = "00000cam a2200000 a 4500";//leader with 24 characters
            myRec.set_LeaderValueByPos("n",5,1);      //set record status
            CControlfield Cf = new CControlfield(); //create new controlfield
            Cf.Tag="001"; //system id
            Cf.Value="13234";
            myRec.Controlfields.Add(Cf); // add controlfield to marc record

            Cf.ReConstruct();//new control field
            Cf.Tag = "008";
            Cf.Value = "                                 ";//34 characters
            Cf.set_ValueByPos("1917", 7, 10);//set Date1
            myRec.Controlfields.Add(Cf); // add controlfield to marc record

            //create Datafields and Subfields
            //100 1_ |a Twain, Mark, |d 1835-1910 |c (Spirit) 
            CDatafield Df = new CDatafield();//new Datafield
            Df.Tag = "100";
            Df.Ind1 = "1";

            CSubfield Sf = new CSubfield();
            Sf.Code = "a";
            Sf.Value = "Twain, Mark";
            Df.Subfields.Add(Sf); //add personal name

            Sf.ReConstruct();
            Sf.Code = "d";
            Sf.Value = "1835-1910";
            Df.Subfields.Add(Sf); //add date

            Sf.ReConstruct();
            Sf.Code = "c";
            Sf.Value = "(Spirit)";
            Df.Subfields.Add(Sf);

            myRec.Datafields.Add(Df);
                        
            //245 10 |a Jap Herron : a novel written from the Ouija board / |c with an introduction, The coming of Jap Herron. 
            Df.ReConstruct();//new Datafield
            Df.Tag = "245";
            Df.Ind1 = "1";
            Df.Ind2 = "0";

            Sf.ReConstruct();
            Sf.Code = "a";
            Sf.Value = "Jap Herron : a novel written from the Ouija board/";
            Df.Subfields.Add(Sf);

            Sf.Code = "c";
            Sf.Value = "with an introduction, The coming of Jap Herron.";
            Df.Subfields.Add(Sf);

            myRec.Datafields.Add(Df);

            //save record to disk
            Console.WriteLine("save marc record to marcrec01.xml");
            myRec.Save("marcrec01.xml");

            Console.WriteLine("load marcxml record from file");

            myRec.load_File("marcrec01.xml");

            //get any data from record

            Console.WriteLine("LDR:" + myRec.Leader);
            for (int i = 0; i < myRec.Controlfields.Count; i++)
            {
                Console.WriteLine(myRec.Controlfields.Controlfield(i).Tag + ":" + myRec.Controlfields.Controlfield(i).Value);
            }
            for (int j = 0; j < myRec.Datafields.Count; j++)
            {
                Console.WriteLine(myRec.Datafields.Datafield(j).Tag + ":");
                Df = myRec.Datafields.Datafield(j);
                for (int k = 0; k < Df.Subfields.Count; k++)
                { 
                Console.WriteLine(String.Format("  {0}:{1}",Df.Subfields.Subfield(k).Code,Df.Subfields.Subfield(k).Value));
                }
            }
            
            //update 
            //find datafield to update
            Console.WriteLine("update record element");
            Df.ReConstruct();
            if (myRec.Datafields.get_Datafield("100", ref Df))
            {
                Df.Tag = "110";
                Df.SubfieldsText = "$aHIENDAI SOFTWARE COMPANY";
            }

            for (int j = 0; j < myRec.Datafields.Count; j++)
            {
                Console.WriteLine(myRec.Datafields.Datafield(j).Tag + ":");
                Df = myRec.Datafields.Datafield(j);
                for (int k = 0; k < Df.Subfields.Count; k++)
                {
                    Console.WriteLine(String.Format("  {0}:{1}", Df.Subfields.Subfield(k).Code, Df.Subfields.Subfield(k).Value));
                }
            }

            Console.Read();
        }
    }
}
